package com.mjfeed.service;

import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;
import android.net.Uri;

import com.mjfeed.bean.ProductJSON;
import com.mjfeed.bean.ProductListJSON;
import com.mjfeed.config.Constants;
import com.mjfeed.dao.ProductDao;
import com.mjfeed.util.HttpUtils;

/**
 * 产品
 * @author zhaominglei
 * @date 2014-12-6
 * 
 */
public class ProductService extends BaseService {
	private ProductDao productDao = new ProductDao();
	
	/**
	 * 所有产品
	 */
	public ProductListJSON getAll() {
		StringBuilder url = new StringBuilder(Constants.PRODUCT_ALL_URL);
		String result = HttpUtils.doGet(url.toString());
		if (result != null && !result.equals("")) {
			try {
				ProductListJSON productList = new ObjectMapper().readValue(result, ProductListJSON.class);
				return (productList != null) ? productList : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}		
		return null;
	}
	
	/**
	 * 产品列表
	 */
	public ProductListJSON getProduct(String referer, String keyword, int page) {
		StringBuilder url = new StringBuilder(Constants.PRODUCT_LIST_URL);
		url.append("?p=").append(page).append("&keyword=").append(Uri.encode((keyword == null)?"":keyword));
		String result = HttpUtils.doGet(url.toString());
		if (result != null && !result.equals("")) {
			try {
				ProductListJSON productList = new ObjectMapper().readValue(result, ProductListJSON.class);
				return (productList != null) ? productList : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 产品列表
	 */
	public ProductListJSON getProduct(Context context, String keyword, int page) {
		if (keyword == null || keyword.equals("")) {
			return productDao.getPagination(context, null, null, null, page, Constants.DEFAULT_PAGE_SIZE);
		} else {
			return productDao.getPagination(context, "name=?", new String[]{ keyword }, null, page, Constants.DEFAULT_PAGE_SIZE);
		}
		
	}
	
	/**
	 * 产品详情
	 */
	public ProductJSON getProductDetail(String referer, Long id) {
		if (id == null) {
			return null;
		}
		StringBuilder url = new StringBuilder(Constants.PRODUCT_DETAIL_URL);
		url.append("?productId=").append(id);
		String result = HttpUtils.doGet(url.toString());
		if (result != null) {
			try {
				ProductJSON detail = new ObjectMapper().readValue(result, ProductJSON.class);
				return (detail != null) ? detail : null; 
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 产品详情
	 */
	public ProductJSON getProductDetail(Context context, Long productId) {
		if (productId == null) {
			return null;
		}
		return productDao.getById(context, productId);
	}
	
	/**
	 * 同步产品
	 */
	public void syncProduct(Context context) {
		ProductListJSON productListJSON = getAll();
		if (productListJSON != null) {
			productDao.deleteAll(context);
			for (ProductJSON productJSON : productListJSON.getRecordList()) {
				productDao.save(context, productJSON);
			}
		}
	}
}
